package tp.ui.menu.pattern;

import java.util.ArrayList;
import java.util.List;

import tp.util.Reader;

public class Menu {

	public List<Menu> getSousMenu() {
		return sousMenu;
	}

	protected String nom;
	protected List<Menu> sousMenu;
	protected String lastMember;

	public Menu(String nom, boolean isSousMenu) {
		lastMember = "Quitter";
		if (isSousMenu) {
			lastMember = "Retour";
		}
		sousMenu = new ArrayList<Menu>();
		this.nom = nom;
	}

	public Menu(String nom) {
		this(nom, false);
	}

	public void print() {
		System.out.println(nom);
		int i = 1;
		for (Menu m : sousMenu) {
			System.out.println("\t" + i + " : " + m.getNom());
			i++;
		}
		System.out.println("\t" + i + " : " + lastMember);
	}

	public void selectionner() {
		int x = -1;
		boolean valid = false;
		while (!valid) {
			x = Reader.lireInt();
			if (x > 0 && x <= sousMenu.size() + 1) {
				valid = true;
			} else {
				System.out.println("veuillez saisir une valeur entre 1 et"
						+ (int) (sousMenu.size() + 1));
			}
		}
		if (x <= sousMenu.size()) {
			sousMenu.get(x - 1).selected();
			selected();
		}
		if (lastMember.equals("Quitter")) {
			System.out.println("FIN DU PROGRAMME");
			System.exit(0);
		}
	}

	public void selected() {
		if (!sousMenu.isEmpty()) {
			print();
			selectionner();

		} else {
			execute();
		}
	}

	public void execute() {

	}

	public void addMenu(Menu m) {
		sousMenu.add(m);
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}
}
